//保存表格的实例对象
var sx = null;
//调用封装方法
getTable();

// ---------------------------------------------------
//方法区

//封装方法重载表格
function sxTable(){
    //通过表格id参数  重载刷新
    sx.reload()
}


//封装渲染表格
function getTable(){
    layui.use(['table','layer','jquery','form','laydate'],function() {
        //模板保存到变量上
        var laydate = layui.laydate;
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.$;

        //监听顶部刷新按钮
        form.on('submit(sx)',function (d){
            location.reload();
        })

        //鉴定顶部返回按钮
        form.on('submit(fh)',function (d){
            layer.msg('返回上一级页面')
        })


        //监听顶部搜索按钮
        form.on('submit(*)',function (d){
            console.log(d.field)
            var data = d.field;
            sx.reload({
                url:'/YltUser/AuditManage/getAuditListByLike',
                where:{
                    userName:data.userName,
                    userAccount:data.userAccount,
                    auditTime:data.auditTime
                }
            },true)
            return false
        })

        //监听顶部重置按钮
        form.on('submit(cz)',function (d){
            $("[name=userName]").val(undefined)
            $("[name=userAccount]").val(undefined)
            $("[name=auditTime]").val(undefined)

            sx.reload({
                url:'/YltUser/AuditManage/getAuditList',
                where:{
                    auditStats: 0
                }
            },true)
            return false
        })

        // 渲染input日期
        laydate.render({
            elem:'#auditTime'
        })

        sx = table.render({
            elem:"#demo",
            url:"/YltUser/AuditManage/getAuditList",
            where:{
                auditStats : 0
            },
            page:{
                layout:['count','prev','page','next','limit'],
                groups:5,
                limit:10,
                limits:[5,10,20]
            },
            cols:
                [[
                    // {field: 'auditId', title: '序号',width: 80,align: 'center'},
                    {field: 'userId', title: '用户编号',width: 100,align: 'center'},
                    {field: 'userAccount', title: '用户账号', width:150,align: 'center'},
                    {field: 'userName', title: '用户昵称',width: 150,align: 'center'},
                    {field: 'applyJob', title: '申请职位', width:120,align: 'center'},
                    {field: 'userPhone', title: '用户电话', width: 150, align: 'center'},
                    {field: 'auditTime', title: '申请时间', width: 200,align: 'center'},
                    {field: 'userEmail', title: '用户邮箱', width: 150,align: 'center'},
                    {title: '操作',toolbar:'#tooldemo',width: 200,align: 'center'}
                ]],
            toolbar:'#toolbardemo',
            skin: 'line',
            parseData:function (res){
                return{
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }

        }),

        //监听表格工具栏
        table.on('tool(filterDemo)',function(obj){
            var data = obj.data;
            if(obj.event == 'sh'){
                layer.confirm('确定要通过'+ data.userName +'的审核请求吗？',function (index){
                    //发送ajax 审核通过
                    $.ajax({
                        type:'post',
                        url:'/YltUser/AuditManage/upAudit',
                        data:{auditId:data.auditId,auditStats:1},
                        dataType:'json',
                        success:function (res){
                            if (res.state == '200'){
                                layer.msg('提交审核通过',{icon:6,time:1000},function (){
                                    layer.close(index);
                                    sxTable();
                                })
                            }else {
                                layer.msg('提交审核失败',{icon:5,time:1000},function (){
                                    layer.close(index);
                                })
                            }
                        }
                    })
                })
            }else if(obj.event == 'bh'){
                layer.open({
                    type:2,
                    title:'审核驳回',
                    content:'bhAudit.html',
                    shade:[1,'#37444B'],
                    anim:4,
                    area:['60%','60%'],
                    //弹出层成功弹出后的回调函数
                    success:function (layero,index){
                        //父页面向子页面传参 -- 向iframe层传数据
                        //获取iframe页面
                        var body = layer.getChildFrame('body',index);
                        body.find('#userId').val(data.userId);
                        body.find('#auditId').val(data.auditId);
                        body.find('#userName').val(data.userName);
                        body.find('#auditTime').val(data.auditTime);
                        body.find('#applyJob').val(data.applyJob);
                        body.find('#userAccount').val(data.userAccount);
                    }
                })
            }
        })
    })

}



